<template>
  <div class="upLoad">
    <div style="padding: 20px 10px">
      <ul v-for="(items, index) in FJData" :key="index">
        <div style="line-height: 30px; border-bottom: 1px solid #d2d2d2; font-size: 17px">
          <van-uploader @click-upload="getList(items)" @click-preview="lookFile" :after-read="afterRead" :before-delete="delectFile" v-model="items.fileList" multiple> </van-uploader>
          <!-- <img v-else-if="!isshow" :src="items.fileList[0].url" alt="" width="100" />
          <van-button v-if="!isshow" size="small" type="info" @click="deleteFuile">重新上传</van-button> -->
        </div>
      </ul>
    </div>
  </div>
</template>

<script>
import {FileServerProxy } from '@/utils/api'
export default {
  data() {
    return {
      groupGuids: [],
      isshow: false
    }
  },
  props: {
    FJData: Array,
    MainObject: Object,//踏勘
  },
  methods: {
    getList(item) {
      this.groupGuids = item.groupGuid
    },
    //相关材料
    afterRead(dataUrl) {
      console.log(dataUrl, 'dataUrl')
      // 此时可以自行将文件上传至服务器dataUrl.content
      this.getFileServerUpload(dataUrl.content, dataUrl.file)
    },
    delectFile: async function (base64Url, file) {
      debugger
      this.ROW_GUID.HXT = "";
      let data1 = {
        'a0e224e8-36be-4591-b770-30cabda3514a': [
          {
            PKID: this.ROW_GUID1.SCOUT_ID,
            ROW_GUID: this.ROW_GUID.ROW_GUID,
            TO_ROW_GUID: this.ROW_GUID.TO_ROW_GUID,
            ...this.ROW_GUID,
            eca_RowState: 'modified'
          }
        ]
      }
      let par = {
        formData: JSON.stringify(data1),
        viewParameter: {}
      }
      let params1 = {
        accessCode: '688bd3883572440a859dfd8ba9194e7b11',
        cityCode: window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '441300000000',
        params: par
      }
      const res = await FileServerProxy(params1)
      let data = JSON.parse(res.data.data).Success
      if (data) {
        this.$toast.success('删除成功!')
      } else {
        this.$toast.success('删除失败!')
      }
    },
    delectFile1: async function (file) {
      if (this.ISDelete) {
        this.$toast.fail('该用户暂不支持删除')
        return false
      }
      let fileguid = ''
      if (file.ROW_GUID) {
        fileguid = file.ROW_GUID
      } else {
        fileguid = this.fileGuid
      }
      if (fileguid) {
        let params = {
          accessCode: 'ce9b691634de4ebca68dc5bb638d07f4',
          cityCode: window.localStorage.getItem('isGJM') == 'true' ? window.localStorage.getItem('citycode') : window.localStorage.getItem('citycodeChange'),
          params: {
            fileGuid: fileguid,
            token: window.localStorage.getItem('token')
          }
        }
        const res = await FileServerProxy(params)
        let data = JSON.parse(res.data.data).Success
        if (data) {
          this.$toast.success('删除成功!')
          this.$emit('delectFile', true)
          //删除this.FJData当前行数据
          this.FJData.forEach((item, index1) => {
            item.FileName.forEach((it, index) => {
              if (it.ROW_GUID == fileguid) {
                item.FileName.splice(index, 1)
              }
            })
          })
        }
      }
    },
    lookFile(item) {
      debugger
      if (item.isImage == false) {
        if (ZWJSBridge) {
          ZWJSBridge.openLink({
            url: item.url
          })
            .then((res) => {})
            .catch((err) => {})
        }
      }
    },
    deleteFuile() {
      this.isshow = true
    },
    getFileServerUpload: async function (base64Url, file) {
      debugger
      // TODO:将base64存储在HXT字段中
      let base64 = base64Url.replace(/^data:image\/\w+;base64,/, '')
      this.MainObject.ZJDZLPMWZT = base64
      let data1 = {
        '1630078c-3fa8-45f9-b3f1-bfd4a3ab918e': [
          {
            PKID: this.MainObject.PKID,
            ROW_GUID: this.MainObject.ROW_GUID,
            ZJDZLPMWZT: base64,
            eca_RowState: 'modified'
          }
        ]
      }
      let par = {
        formData: JSON.stringify(data1),
        viewParameter: {}
      }
      let params1 = {
        accessCode: '688bd3883572440a859dfd8ba9194e7b11',
        cityCode: window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '',
        params: par
      }
      const res = await FileServerProxy(params1)
      let data = JSON.parse(res.data.data).Success
      if (data) {
        this.$toast.success('保存成功')
      } else {
        this.$toast.success('保存失败')
      }
    },
    handleLJD(data) {
      if (data.url == '') {
        this.$toast.fail('暂无数据')
      } else {
        if (ZWJSBridge) {
          this.$toast.fail('暂不支持预览')
        } else {
          const link = document.createElement('a')
          const evt = document.createEvent('HTMLEvents')
          evt.initEvent('click', false, false)
          link.href = data.url
          link.target = '_blank'
          link.style.display = 'none'
          document.body.appendChild(link)
          link.click()
          window.URL.revokeObjectURL(link.href)
        }
      }
    }
  },
  mounted() {
    // this.getFile1()
    console.log(this.FJData, this.ROW_GUID1, 'djfbubn')
  }
}
</script>

<style scoped lang="scss"></style>
